package com.yq.ylmall.exception;

import com.yq.ylmall.result.Result;
import com.yq.ylmall.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 统一异常处理类
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<Object> error(Exception e){
        e.printStackTrace();
        log.error("程序异常：{}", e.getMessage());
        return Result.fail(ResultCode.SERVER_ERROR);
    }

    @ExceptionHandler(ServiceException.class)
    @ResponseBody
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result<Object> serviceExceptionHandler( ServiceException e){
        log.error("服务异常：{}", e.getMessage());
        return Result.fail(e.getResultCode());
    }

}
